package org.wanho.shop.util;

import org.wanho.shop.exception.MD5ErrorException;
import sun.misc.BASE64Encoder;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Author：汤小洋
 * Date：2023-04-26 10:57
 * Description：<描述>
 */
public class MD5Util {

    public static String md5(String s){
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] bytes = md.digest(s.getBytes());
            BASE64Encoder encoder = new BASE64Encoder();
            return encoder.encode(bytes);
        } catch (NoSuchAlgorithmException e) {
            throw new MD5ErrorException("MD5加密错误！");
        }
    }

    public static void main(String[] args) {
        System.out.println(MD5Util.md5("123"));
    }
}
